//
//  HMWelcomeViewController.swift
//  WeiBo14
//
//  Created by HM on 16/3/5.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
import SDWebImage

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    // 设置视图
    private func setupUI(){
        view.backgroundColor = UIColor.whiteColor()
        
        // 添加子控件
        view.addSubview(headImage)
        view.addSubview(messageLabel)
        
        // 添加约束
        
        headImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage)
            make.top.equalTo(headImage.snp_bottom).offset(20)
        }
    }
    
    // 做动画一定要在界面显示完成之后再去
    // 1,可以界面没有显示完成去做动画的话,会出现意想不到的问题
    // 2,都没显示出来,需要去做动画??
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 先更新约束
        headImage.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(100)
        }
        // 执行动画

        // Dampin 阻尼: 0 -> 1
        // Velocity: 弹簧的初始速度
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
        }) { (_) -> Void in
            // 动画执行完毕,执行label显示的动画
            UIView.animateWithDuration(1, animations: { () -> Void in
                self.messageLabel.alpha = 1
            }, completion: { (_) -> Void in
                // label的透明动画执行完毕
                // 跳转到首页里面去
                print("跳转界面,跳转到 HMMainViewController 里面")
                
//                // 1. 获取到 appdelegate 里面的window
//                let delegate = UIApplication.sharedApplication().delegate! as! AppDelegate
//                print(delegate)
//                let window = delegate.window
//                // 2. 设置根控制器
//                window?.rootViewController = HMMainViewController()
                
                // 发送一个切换根控制器的通知,Appdelegate收到之后就去切换
                NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootVCNotification, object: self)
                
            })
        }
    }
    
    // MARK: - 懒加载控件
    
    // 头像
    private lazy var headImage: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        let headImageUrlString = HMUserAccountViewModel.sharedAccountViewModel.account?.avatar_large
        imageView.sd_setImageWithURL(NSURL(string: headImageUrlString!), placeholderImage: UIImage(named: "avatar_default_big"))
        return imageView
    }()
    
    // 欢迎回来的label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
        label.text = "欢迎回来"
        label.sizeToFit()
        label.alpha = 0
        return label
    }()

}
